package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.shop.entity.ShopItemSpecs;
import com.yunc.modules.shop.service.IShopItemSpecsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:30
 */
@RestController
@RequestMapping("itemSpecs")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopItemSpecsController {

    private final IShopItemSpecsService shopItemSpecsService;
    
    @GetMapping("/page")
    public R page(Page<ShopItemSpecs> page, ShopItemSpecs itemSpecs){
        shopItemSpecsService.page(page, new QueryWrapper<>(itemSpecs));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ShopItemSpecs itemSpecs){
        QueryWrapper<ShopItemSpecs> ew = new QueryWrapper<>();
        List<ShopItemSpecs> list = shopItemSpecsService.list(ew);
        return R.ok().put("list", list).put("itemSpecs", itemSpecs);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        ShopItemSpecs itemSpecs = shopItemSpecsService.getById(id);
        return R.ok().put("itemSpecs", itemSpecs);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(ShopItemSpecs itemSpecs){
        boolean flag = shopItemSpecsService.save(itemSpecs);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(ShopItemSpecs itemSpecs){
        boolean flag = shopItemSpecsService.updateById(itemSpecs);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
